package com.juvenxu.mvnbook.account.service;
public class AccountServiceImpl implements AccountService{
	private AccountPersistService accountPersistService;
	private AccountEmailService accountEmailService;
	private AccountCaptchaService accountCaptchaService;
	
	private Map<String,String> activatiomMap=new HashMap<String,String>();
	public void setAccountPersistService(AccountPersistService accountPersistService){
		this.accountPersistService=accountPersistService;
	}
	public AccountPersistService getAccountPersistService(){
		return accountPersistService;
	}
	public void setAccoutEmailService(AccountEmailService accounEmailService){
		this.accountEmailService=accountEmailService;
	}
	public AccountEmailService getAccountEmailService(){
		return accountEmailService;
	}
	public void setAccountCaptchaService(AccountCaptchaService accountCaptchaService){
		this.accountCaptchaService=accountCaptchaService;
	} 
	public AccountCaptchaService getAccountCaptchaService(
		return accountCaptchaService;	
	)

	public String generateCaptchaKey()throws AccountServiceException{
		try{
			return accountCaptchaService.generateCaptchaKey();
		}catch(AccountCaptchaException e){
			throw new AccountCaptchaException("Unable to generate Captcha key",e);
		}
	}
	public byte[] generateCaptchaImage(String captchaKey)throws AccountServiceException{
		try{
			return accountCaptchaService.generateCaptchaImage(captchaKey);
		}catch(AccountCaptchaException e){
			throw new AccountServiceException)("Unable to generate Captcha Image",e);
		}
	}
	public void signUp(SignUpRequest signUpRequest)throws AccountServiceException{
		try{
			if(!signUpRequest.getPassword().equals(signUpRequest.getConfirmPassword())){
				throw new AccountServiceException("password do not match");
			}
			if(!accountCaptchaService.validateCaptcha(signUpRequest.getCaptchaKey(),signUpRequest.getCaptchaValue())){
				throw new AccountServiceException("incorrect Captcha.");
			}
			Account account=new Account();
			account.setId(signUpRequest.getId());
			account.setName(signUpRequest.getName());
			account.setEmail(signUpRequest.getEmail());
			account.setPassword(signUpRequest.getPassword());
			account.setActivated(false);

			accountPersistService.createAccount(account);
			String activationId=RandomGenerator.getRandomString();
			activationMap.put(activationId,account.getId());
		
			String link=signUpRequest.getActivateServiceUrl().endsWith("/")?signUpRequest.getActivateService()+activationId:signUpRequest.getActivateServiceUrl()+"?key="+activationId;

			accountEmailService.sendMail(account.getEmail(),"please activate your account",link);
		}catch(AccountCaptchaException e){
			throw new AccountServiceException("Unable to validate captcha",e);
		}catch(AccountPersistException e){
			throw new AccountServiceException("Unable to create account",e);
		}catch(AccountEmailException e){
			throw new AccountServiceException("Unable to send activation mail",e);
		}
	}
	public void activate(String activationId)throws AccountServiceException{
		String accountId=activationMap.get(activationId);
		if(accountId==null){
			throw new AccountServiceException("Invalid account activation ID");
		}
		try{
			Account account =accountPersistService.readAccount(accountId);
			account.setActivated(true);
			accountPersistService.updateAccount(account);
		}catch(AccountPersistException e){
			throw new AccountServiceException("Unable to activate account");
		}
	}
	public void login(String id,String password)throw AccountServiceException{
		try{
			Account account=accountPersistServiceException.readAccount(id);
			if(account==null){
				throw new AccountServiceException("Account does no exist");
			}
		}
		if(!account.getActivated()){
			throw new AccountServiceException("account is disable");
		}
		if(!account.getPassword().equals(password)){
			throw new AccountServiceException("Incorrect password");
		}
	}catch(AccountPersistException e){
		throw new AccountServiceException("Unable to login",e);
	}
}